﻿' --------------------
'修改时间：2020-12-17
'修改内容：Instant0改为主动电机，Instant1改为负载电机，用AD暂时改用2,3通道
'--------------------

Imports Automation.BDaq
Imports System.ComponentModel

Public Class TorqueSensor

    Public Shared Torques As AdDevice = Nothing
    Public InitTime As Date

    Private MasterQueue As Queue(Of Double) = New Queue(Of Double)
    Private LoadQueue As Queue(Of Double) = New Queue(Of Double)
    Private TimeQueue As Queue(Of Double) = New Queue(Of Double)

    <Description("负载电机转矩，10ms内的均值")>
    ReadOnly Property Load As Double
        Get
            Return Torques.Instant1
        End Get
    End Property

    <Description("主动电机转矩，10ms内的均值")>
    ReadOnly Property Master As Double
        Get
            Return Torques.Instant0
        End Get
    End Property

    <Description("数据队列的长度")>
    Property QueueLength As Integer

    <Description("主动电机脉冲计数，60个一圈")>
    ReadOnly Property MasterCount As Long
        Get
            Return EventCounter.GetCount(0)
        End Get
    End Property

    <Description("负载电机脉冲计数，60个一圈")>
    ReadOnly Property LoadCount As Long
        Get
            Return EventCounter.GetCount(1)
        End Get
    End Property


    ''' <summary>
    ''' 构造函数
    ''' </summary>
    ''' <param name="start_ch">第一个通道编号</param>
    Public Sub New(start_ch As Integer)
        If Torques Is Nothing Then
            Torques = New AdDevice(PCIECard.FindCard("Demo"), start_channel:=start_ch)
            EventCounter.InitCounter(2, Torques.card.SelectedDevice)
            QueueLength = 1000
            InitTime = Date.Now
        Else
            Throw New ArgumentException("重复实例化AD板卡")
        End If
    End Sub

    ''' <summary>
    ''' 修改两个传感器变比，变比=传感器量程/10
    ''' </summary>
    ''' <param name="master_ratio">小电机变比</param>
    ''' <param name="slave_ratio">大电机变比</param>
    Public Sub SetRatio(master_ratio As Double, slave_ratio As Double)
        Dim ratios As Double() = {master_ratio, slave_ratio}
        Torques.SetRatios(ratios)
    End Sub

    Public Sub Begin(Optional rate As Integer = 10000, Optional section_len As Integer = 100)
        If Torques IsNot Nothing Then
            Torques.Begin(rate, section_len)
            EventCounter.Start()
        End If
    End Sub

    Public Sub [Stop]()
        If Torques IsNot Nothing Then
            Torques.Stop()
        End If
        EventCounter.Stop()
    End Sub

    ''' <summary>
    ''' 将转矩数据存入队列
    ''' </summary>
    Public Sub PushData()
        If TimeQueue.Count >= QueueLength Then
            TimeQueue.Dequeue()
            MasterQueue.Dequeue()
            LoadQueue.Dequeue()
        End If

        If TimeQueue.Count < QueueLength Then
            TimeQueue.Enqueue((Date.Now - InitTime).TotalSeconds)
            MasterQueue.Enqueue(Master)
            LoadQueue.Enqueue(Load)
        End If
    End Sub

    Public Function GetTimeBuf() As Double()
        Return TimeQueue.ToArray()
    End Function

    Public Function GetMasterBuf() As Double()
        Return MasterQueue.ToArray()
    End Function

    Public Function GetLoadBuf() As Double()
        Return LoadQueue.ToArray()
    End Function

End Class
